import json
import time

import requests
from bs4 import BeautifulSoup as bs
import false_email


class HtmlDownloader(object):
    def __init__(self):
        self.send_mail = false_email.FalseEmail()

    def download(self, url, sleep_time=8, num_retries=2):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest'}
        # proxies = {
        #     'https': '221.226.82.130:8998',
        #     'https': '121.193.143.249:80',
        #     'https': '124.88.67.17:82'
        # }
        time.sleep(sleep_time)
        try:
            page_one = requests.get(url, headers=headers)
            return page_one
        except:
            self.send_mail.send_email(content=url,name='lives')
            if num_retries > 0:
                return self.download(url, num_retries - 1)

    def zq_dict(self, new_url):
        response = self.download(new_url)
        try:
            return response.json()['data']['rooms']
        except:
            return 1

    def xm_dict(self, new_url):
        response = self.download(new_url)
        try:
            return response.json()['data']['items']
        except:
            return 1

    def dy_soup(self, new_url):
        response = self.download(new_url, sleep_time=5)
        soup = bs(response.text, 'html.parser')
        return soup
